package com.zuxia.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.opensymphony.xwork2.ActionContext;
import com.zuxia.iservices.InventoryinfoServicesI;
import com.zuxia.po.Cardtypeinfo;
import com.zuxia.po.Inventoryinfo;
import com.zuxia.po.Partstypeinfo;

public class InventoryinfoAction extends BaseAction{
	private InventoryinfoServicesI inventoryinfoServices;
	private Inventoryinfo inventoryinfo;
	private Partstypeinfo partstypeinfo;
	private Cardtypeinfo cardtypeinfo;
	 // 声明页码
    private Integer page=1;
    //每页条数
    private Integer tr;
    
    // 声明模糊查询的值
    private String itiname;
    
    //封装上传文件的属性
    private File upload;
    
    //接收依赖注入的属性,文件路径
    private String savePath="/admin/images";
    
    //封装上传文件名的属性
    private String uploadFileName;
    
    
    

    
	public String getUploadFileName() {
		return uploadFileName;
	}


	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}


	public File getUpload() {
		return upload;
	}


	public void setUpload(File upload) {
		this.upload = upload;
	}


	public String getSavePath() {
		return savePath;
	}


	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}


	public Integer getTr() {
		return tr;
	}


	public void setTr(Integer tr) {
		this.tr = tr;
	}


	public Integer getPage() {
		return page;
	}


	public void setPage(Integer page) {
		this.page = page;
	}

	public String getItiname() {
		return itiname;
	}


	public void setItiname(String itiname) {
		this.itiname = itiname;
	}


	public Inventoryinfo getInventoryinfo() {
		return inventoryinfo;
	}


	public Partstypeinfo getPartstypeinfo() {
		return partstypeinfo;
	}

	public void setPartstypeinfo(Partstypeinfo partstypeinfo) {
		this.partstypeinfo = partstypeinfo;
	}





	public Cardtypeinfo getCardtypeinfo() {
		return cardtypeinfo;
	}





	public void setCardtypeinfo(Cardtypeinfo cardtypeinfo) {
		this.cardtypeinfo = cardtypeinfo;
	}





	public void setInventoryinfo(Inventoryinfo inventoryinfo) {
		this.inventoryinfo = inventoryinfo;
	}





	public InventoryinfoServicesI getInventoryinfoServices() {
		return inventoryinfoServices;
	}





	public void setInventoryinfoServices(
			InventoryinfoServicesI inventoryinfoServices) {
		this.inventoryinfoServices = inventoryinfoServices;
	}
	
	
	


	public void pageCount(){
		int pageCount=inventoryinfoServices.count(itiname);
		String json=JSON.toJSONString(pageCount);
		System.out.println("总数："+json);
		try {
			getResponse().getWriter().print(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	/**
	 * 查询库存信息
	 */
	public void findAll() {
		List<Inventoryinfo> list=inventoryinfoServices.selAllInventoryinfoPageModel(page, tr, itiname);
		List list1=new ArrayList<Map<String,String>>();
		Map<String,String> data=null;
		for(int i=0;i<list.size();i++){
			data=new HashMap<String,String>();
			Inventoryinfo it=list.get(i);
			data.put("itiname",it.getItiname());
			data.put("itiprice",Integer.toString(it.getItiprice()));
			data.put("itiselling",Integer.toString(it.getItiselling()));
			data.put("iTIPicture",it.getItipicture());
			data.put("iTIParameter",it.getItiparameter());
			data.put("iTIEffect",it.getItieffect());
			data.put("iTICount",Integer.toString(it.getIticount()));
			data.put("ptitypeName",it.getPartstypeinfo().getPtitypeName());
			data.put("ctitype",it.getCardtypeinfo().getCtitype());
			data.put("itItiid",Integer.toString(it.getItiid()));
			list1.add(data);
		}
		if (list!=null) {
			//转化为json字符串发送给前台
			System.out.println(list1);
			String json=JSON.toJSONString(list1);
			try {
				getResponse().getWriter().print(json);
				System.err.println("查询到的库存json字符串如下！"+json);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 添加库存信息
	 * @return 
	 * @throws IOException 
	 * @throws Exception 
	 */
	@SuppressWarnings("all")
	public String save() throws IOException{
		FileOutputStream fos;
		try {
			 //将图片上传到工作路径
			String sp=ServletActionContext.getServletContext().getRealPath("/")+getSavePath() + "\\" +getUploadFileName();
			fos = new FileOutputStream(sp);
			//以上传文件建立一个文件上传流
			FileInputStream fis=new FileInputStream(getUpload());
			//将上传文件的内容写入服务器
			byte[] buffer=new byte[1024];
			int len=0;
			while((len=fis.read(buffer))>0){
				fos.write(buffer,0,len);
			}
			inventoryinfo.setItipicture(getUploadFileName());
			inventoryinfo.setPartstypeinfo(partstypeinfo);
			inventoryinfo.setCardtypeinfo(cardtypeinfo);
			System.out.println("即将插入的信息如下：--"+inventoryinfo.getCardtypeinfo().getCtiid()+"--"+inventoryinfo.getPartstypeinfo().getPtiid()+"--------");
			inventoryinfoServices.save(inventoryinfo);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "index";
	}
	
	
	/**
	 * 删除库存信息
	 */
	public void delectInventoryinfo(){
		System.out.println(inventoryinfo.getItiid());
		Boolean b=inventoryinfoServices.delectInventoryinfo(inventoryinfo.getItiid());
		try {
			getResponse().getWriter().print(b);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 修改库存信息
	 * @throws IOException 
	 */
	public String updateInventoryinfo() throws IOException{
		if(upload==null){
			inventoryinfo.setPartstypeinfo(partstypeinfo);
			inventoryinfo.setCardtypeinfo(cardtypeinfo);
			inventoryinfoServices.updateInventoryinfo(inventoryinfo);
			return "index";
		}
		FileOutputStream fos;
		try {
			 //将图片上传到工作路径
			String sp=ServletActionContext.getServletContext().getRealPath("/")+getSavePath() + "\\" +getUploadFileName();
			fos = new FileOutputStream(sp);
			//以上传文件建立一个文件上传流
			FileInputStream fis=new FileInputStream(getUpload());
			//将上传文件的内容写入服务器
			byte[] buffer=new byte[1024];
			int len=0;
			while((len=fis.read(buffer))>0){
				fos.write(buffer,0,len);
			}
			inventoryinfo.setItipicture(getUploadFileName());
			inventoryinfo.setPartstypeinfo(partstypeinfo);
			inventoryinfo.setCardtypeinfo(cardtypeinfo);
			System.out.println(inventoryinfo.getItiid());
			System.out.println("即将修改的信息如下：--"+inventoryinfo.getCardtypeinfo().getCtiid()+"--"+inventoryinfo.getPartstypeinfo().getPtiid()+"--------");
			boolean b=inventoryinfoServices.updateInventoryinfo(inventoryinfo);
			getResponse().getWriter().print(b);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			return "index";
	}
	/**
	 * 查询ID信息
	 */
	public void selectiTIId() {
		List<Inventoryinfo> list=inventoryinfoServices.selectiTIId(inventoryinfo.getItiid());
		List list1=new ArrayList<Map<String,String>>();
		Map<String,String> data=null;
			data=new HashMap<String,String>();
			Inventoryinfo it=list.get(0);
			data.put("itiname",it.getItiname());
			data.put("itiprice",Integer.toString(it.getItiprice()));
			data.put("itiselling",Integer.toString(it.getItiselling()));
			data.put("iTIPicture",it.getItipicture());
			data.put("iTIParameter",it.getItiparameter());
			data.put("iTIEffect",it.getItieffect());
			data.put("iTICount",Integer.toString(it.getIticount()));
			data.put("ptitypeName",it.getPartstypeinfo().getPtitypeName());
			data.put("pTIId",Integer.toString(it.getPartstypeinfo().getPtiid()));
			data.put("ctitype",it.getCardtypeinfo().getCtitype());
			data.put("cTIId", Integer.toString(it.getCardtypeinfo().getCtiid()));
			data.put("itItiid",Integer.toString(it.getItiid()));
			list1.add(data);
		if (list!=null) {
			//转化为json字符串发送给前台
			System.out.println(list1);
			String json=JSON.toJSONString(list1);
			try {
				getResponse().getWriter().print(json);
				System.err.println("查询到的库存json字符串如下！"+json);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
